package top.frsf.pattern.a_creational.b_factory.a_simple_factory;

import top.frsf.pattern.a_creational.b_factory.a_simple_factory.service.Payment;
import top.frsf.pattern.a_creational.b_factory.a_simple_factory.service.impl.*;


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 简单工厂模式 - 单例模式
 *
 * @author He Kuntao 
 * @since  2025/4/22 15:07
 */
public class SingletonPaymentFactory {

    private static final Map<String, Payment> INSTANCES = new ConcurrentHashMap<>();

    static {
        registerSingleton("app", new AppPayment());
        registerSingleton("vx", new VxPayment());
        registerSingleton("zfb", new ZfbPayment());
    }

    public static void registerSingleton(String type, Payment instance) {
        INSTANCES.put(type, instance);
    }

    /**
     * 获取支付实例
     *
     * @param type 支付类型
     * @return 支付实例
     */
    public static Payment getPayment(String type) {
        Payment instance = INSTANCES.get(type);
        if (instance == null) {
            throw new IllegalArgumentException("Unknown payment type: " + type);
        }
        return instance;
    }
}